package com.DFB.controller;


import com.DFB.common.model.Result;
import com.DFB.common.model.ResultCode;
import com.DFB.pojo.Product;
import com.DFB.pojo.StaffMerchant;
import com.DFB.pojo.dto.ProductDto;
import com.DFB.service.ProductService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品表(菜品表) 前端控制器
 * </p>
 *
 * @author DFB
 * @since 2023-09-11
 */
@RestController
@Slf4j
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     * 查询对应商户下所有上架商品
     * 2.查询库存不足商品（库存小于5）
     * 3.查询已售罄商品
     * 4.查询已下架商品
     * 5.查询已删除商品
     * 6.商品名称部分字搜索商品
     * 7.分类查搜索品根据商品一级分类字段搜索分类下商品
     *
     * @param storeId
     * @return
     */
    @GetMapping("/productList/{storeId}")
    public Result<?> getProductList(@PathVariable("storeId") Integer storeId) {
        try {
            // 查询列表
            QueryWrapper<Product> wrapper = new QueryWrapper<>();
            wrapper.eq("store_id", storeId);
            List<Product> list = productService.list(wrapper);
            return Result.success(list);
        } catch (Exception e) {
            log.info("Error occurred while select product: " + e.getMessage());
            return Result.error(ResultCode.DATA_IS_WRONG);
        }
    }

    /**
     * 添加商品
     *
     * @param product
     * @return
     */
    @PostMapping("/addProduct")
    public Result<?> addProduct(@RequestBody Product product) {
        try {
            productService.save(product);
            return Result.success(product);
        } catch (Exception e) {
            log.info("Error occurred while adding product: " + e.getMessage());
            return Result.error(ResultCode.DATA_ALREADY_EXISTED);
        }
    }

    /**
     * 修改商品
     *
     * @param product
     * @return
     */
    @PostMapping("/updateProduct")
    public Result<?> updateProduct(@RequestBody Product product) {
        try {
            productService.updateById(product);
            return Result.success(product);
        } catch (Exception e) {
            log.info("Error occurred while updating product: " + e.getMessage());
            return Result.error(ResultCode.DATA_IS_WRONG);
        }
    }

    /**
     * 删除员工
     *
     * @param staffId
     * @return
     */
    @PostMapping("/delProduct")
    public Result<?> delProduct(@RequestBody Integer staffId) {
        try {
            productService.removeById(staffId);
            return Result.success();
        } catch (Exception e) {
            log.info("Error occurred while delete product: " + e.getMessage());
            return Result.error(ResultCode.RESULT_DATA_NONE);
        }
    }

}
